package controller;

import entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import entity.Result;
import service.TokenService;
import service.UserService;

/**
 * @program: SSM201759225309
 * @description: Token控制层
 * @author: Xiaozhuoer
 * @create: 2021-05-15 19:18
 **/

@Controller
@RequestMapping("token")
public class TokenController {

    private final TokenService tokenService;
    private final UserService userService;
    public TokenController(TokenService tokenService, UserService userService){
        this.userService = userService;
        this.tokenService = tokenService;
    }

    /**
     * 检测Redis中是否存在该UUID
     * @param uuid
     * @return Result
     */
    @RequestMapping("/success")
    @ResponseBody
    public Result success(String uuid){
        Result result = new Result();
        if(tokenService.CheckToken(uuid)){
            result.setStatus(0);
            result.setMsg("UUID存在");
            result.setData(null);
        }
        return result;
    }
    /**
     * 通过UUID获取User的Type
     * @param uuid
     * @return Result
     */
    @RequestMapping("/selectTypeByUuid")
    @ResponseBody
    public Result selectTypeByUuid(String uuid){
        Result result = new Result();
        if(tokenService.CheckToken(uuid)){
            String tokenString = tokenService.getTokenString(uuid);
            //JSONObject jsonToken = (tokenString);
            String type = tokenService.getTypeByTokenString(tokenString);
            result.setMsg("type");
            result.setStatus(0);
            result.setData(type);
        }else{
            result.setStatus(1);
            result.setMsg("UUID不存在");
            result.setData(null);
        }
        return result;
    }
    /**
     * 通过UUID获取User信息
     * @param uuid
     * @return Result
     */
    @RequestMapping("/findUserAllByUuid")
    @ResponseBody
    public Result findUserAllByUuid(String uuid){
        Result result = new Result();
        if(tokenService.CheckToken(uuid)){
            String tokenString = tokenService.getTokenString(uuid);
            String type = tokenService.getTypeByTokenString(tokenString);
            String username = tokenService.getUserNameByTokenString(tokenString);
            String email = tokenService.getEmailByTokenString(tokenString);
            User user = new User();
            user.setType(Integer.parseInt(type));
            user.setUsername(username);
            user.setEmail(email);
            result.setMsg("type");
            result.setStatus(0);
            result.setData(user);
        }else{
            result.setStatus(1);
            result.setMsg("UUID不存在");
            result.setData(null);
        }
        return result;
    }
    /**
     * 通过UUID,去更新User的Email
     * @param uuid,email
     * @return Result
     */
    @RequestMapping("/updateEmailByUuid")
    @ResponseBody
    public Result updateEmailByUuid(String uuid,String email){
        Result result = new Result();
        if(tokenService.CheckToken(uuid)){
            String tokenString = tokenService.getTokenString(uuid);
            String id = tokenService.getIdByTokenString(tokenString);
            User user = userService.findUserById(Integer.parseInt(id));
            user.setEmail(email);
            result = userService.modifyEmail(user);
            tokenService.updateValue(uuid,user);

        }else{
            result.setStatus(1);
            result.setMsg("UUID不存在");
            result.setData(null);
        }
        return result;
    }
    /**
     * 通过UUID,去更新User的Type
     * @param uuid,type
     * @return Result
     */
    @RequestMapping("/updateTypeByUuid")
    @ResponseBody
    public Result updateTypeByUuid(String uuid,int type){
        Result result = new Result();
        if(tokenService.CheckToken(uuid)){
            String tokenString = tokenService.getTokenString(uuid);
            String id = tokenService.getIdByTokenString(tokenString);
            User user = userService.findUserById(Integer.parseInt(id));
            user.setType(type);
            result = userService.modifyType(user);
            tokenService.updateValue(uuid,user);
        }else{
            result.setStatus(1);
            result.setMsg("UUID不存在");
            result.setData(null);
        }
        return result;
    }
}
